package server

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"specifica_video_manage/client"
	"specifica_video_manage/model"
)

type RecordServer struct{}

func (r RecordServer) GetRecord(c *gin.Context) {
	queryUrl := c.Query("url")
	if len(queryUrl) > 0 {
		fmt.Println("queryUrl:", queryUrl)
		result := client.MysqlTaskClient{}.QueryRecordUrl(queryUrl)
		if result {
			c.JSON(200, model.Resp{}.Scrapyed("url already scraped"))
			return
		}
		c.JSON(200, model.Resp{}.Empty("not scraped the url"))
		return
	} else {
		// not get the url
		fmt.Println("error url params")
		c.JSON(200, model.Resp{}.Error("error url params, not find any url"))

	}

}

func (r RecordServer) AddRecord(c *gin.Context) {
	recordInfo := model.UrlRecord{}
	err := c.BindJSON(&recordInfo)
	if err != nil {
		fmt.Println("AddRecord bind json catch error:", err.Error())
		c.JSON(500, model.Resp{}.Error(err.Error()))
		return
	}
	go client.MysqlTaskClient{}.CreateUrl(&recordInfo)
	c.JSON(200, model.Resp{}.Received("has received the url: "+recordInfo.Url))
}
